package com.changanw.permiss.comfig;

import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Collection;

/**
 * @author changanw
 * @version ncc1.0
 * @description 自定义的权限评估器
 * @date 2023/6/27 20:49
 */
//@Component
public class CustomerPermissionEvaluator implements PermissionEvaluator {
  @Override
  public boolean hasPermission(Authentication authentication, Object targetDomainObject, Object permission) {
    //获取用户的全部权限
    Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
    for (GrantedAuthority authority : authorities) {
      if (authority.getAuthority().equals(permission)){
        //说明当前登录的用户具备当前方法的操作权限
        return true;
      }
    }

    return false;
  }

  @Override
  public boolean hasPermission(Authentication authentication, Serializable targetId, String targetType, Object permission) {
    return false;
  }
}
